#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//创建结构体
typedef struct movie{
	int id;
	char name[20];
	float grade;
}KCL;


typedef  struct dul{
	KCL mov;
	struct dul *next;
}MOV;



MOV *cret(void);  //创建头节点
MOV *sun(MOV *head,int n);     //新建链表
void For(MOV *head);   //遍历
void read(MOV *head);   //读取文件 
MOV *inread(MOV* head);  //返回
void del(MOV* head);     //释放
void Save(MOV* head);   //存入




int main(){
	int n=0;
	MOV* head = cret();
	printf("1\n");
	 sun(head,n);
	 printf("555");
	read(head);
	printf("2\n");//尾插
	For(head);
    printf("3\n")	;//遍历
	del(head);
    printf("4\n");
	return 0;
}


//创建头节点
MOV *cret(void){
	printf("aaaa");
	MOV *head=NULL;
	head=(MOV*)malloc(sizeof(MOV));
	memset(head,0,sizeof(MOV));
	head ->next =NULL;
	return head;
}
//创建链表
MOV *sun(MOV *head,int n){ 
	//printf("bbbbb");
	int count =0,flag=0;

	printf("*****\n");
	MOV *p=head;
	while(1){
		count++;
		MOV *point=malloc(sizeof(MOV));
		memset(point,0,sizeof(MOV));
		scanf("%d%s%f",&point->mov.id,point ->mov.name,&point->mov.grade);
		printf("id=%d\n name=%s\n grade=%f\n",point->mov.id,point->mov.name,point->mov.grade);
		if(count==2){
			flag=1;
		}       
		p->next =point;
		point->next=NULL;
		p=point;
	}
	return head;
}





void For(MOV *head){
  printf("ccc\n");
	if(head==NULL){
		return ;
	}
	MOV *p=head->next;
	while(p!=NULL){
		printf("id=%d\n name=%s\n grade=%f\n",p->mov.id,p->mov.name,p->mov.grade);
		p=p->next;}
}




void Save(MOV* head){
//	printf("dddddddddddddddddddd");
	FILE* fp=NULL;
	fp=fopen("./姚.txt","w");
	if(fp==NULL){
		perror("fopen");
		exit(1);
	}
	MOV *p=head->next;
	while(p!=NULL){
		fwrite(p,sizeof(MOV),1,fp);
		fprintf(fp,"id=%d\n name=%s\n grade=%f\n",p->mov.id,p->mov.name,p->mov.grade);
		p=p->next;
	}
	fclose(fp);
}




void read(MOV *head){
	printf("bbb\n");
	FILE *fp=NULL;
	fp=fopen("./姚.txt","r");
	if(fp==NULL){
		perror("fopen");
		exit(1);
	}
	MOV Mov;
	while(!feof(fp)){
		fread(&Mov,sizeof(MOV),1,fp);
	printf("id=%d\n name=%s\n grade=%f\n",Mov.mov.id,Mov.mov.name,Mov.mov.grade);

	}
	fclose(fp);
}



MOV *inread(MOV* head){
	//printf("fffffffffffffffff");
	FILE *fp=NULL;
	MOV *node=NULL;
	MOV *end=head;
	fp=fopen("./姚.txt","r");
	if(fp=NULL){
		perror("fopen");
		exit(1);
	}
	size_t rd=0;
	while(!feof(fp)){
		node =(MOV*)malloc(sizeof(MOV));
		memset(node,0,sizeof(MOV));
		rd=fread(node,sizeof(MOV),1,fp);
		if(rd==0){
			free(node);
			node=NULL;
			break;
		}
		printf("id=%d\n name=%s\n grade=%f\n",node->mov.id,node->mov.name,node->mov.grade);
		end->next=node;
		node->next=NULL;
		end=end->next;

	}
	fclose(fp);
	return head;

}
void del(MOV*head)
{
	printf("ddd\n");
	MOV*cur = NULL;
	MOV*temp = head;
	while(temp!=NULL)
	{
		cur = temp->next;
		free(temp);
		temp = NULL;
		temp = cur;
	}
}
